package com.garbagecan.cachestudy.ehcache;


import java.net.URL;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class PerformanceTest2 {

	private CacheManager cacheManager;
	
	private int count = 1 * 10000;
	
	public static void main(String[]args) {
		System.out.println("java.io.tmpdir:" + System.getProperty("java.io.tmpdir"));

		PerformanceTest2 test = new PerformanceTest2();
		test.start();
		
		test.writeToCache();
		test.readFromCache();
		
		test.stop();
	}

	private void start() {
		URL url = PerformanceTest2.class.getResource("/ehcache.xml");
		cacheManager = new CacheManager(url);
		System.out.println(cacheManager.getDiskStorePathManager());
	}

	private void stop() {
		cacheManager.shutdown();
	}
	
	private void writeToCache() {
		long start = System.currentTimeMillis();
		Cache cache = cacheManager.getCache("sampleCache1");
		for (int i = 0; i < count; i++) {
			cache.put(new Element("key_" + i, "value_" + i));
		}
		long stop = System.currentTimeMillis();
		System.out.println(stop - start);
	}
	
	private void readFromCache() {
		System.out.println("cache name size:" + cacheManager.getCacheNames().length);
		for (String name : cacheManager.getCacheNames()) {
			System.out.println("cache name: " + name);
		}
		
		long start = System.currentTimeMillis();
		Cache cache = cacheManager.getCache("sampleCache1");
		System.out.println(cache);
		List<?> keys = cache.getKeys();
		for (int i = 0; i < keys.size(); i++) {
			Object key = keys.get(i);
			Object value = cache.get(key).getObjectValue();
			System.out.println(key + ":" + cache.get(key));
			//log.info(key + ":" + value);
		}
		long stop = System.currentTimeMillis();
		System.out.println(stop - start);
	}
}
